//
//  TravelViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "TravelViewController.h"

#import "CustomTravView.h"
#import "UIImageView+WebCache.h"

@interface TravelViewController ()

@end

@implementation TravelViewController

- (void)dealloc
{
    [_image release];
    [_model release];
    [_ssDic release];
    [_travScroll release];
    [_travelArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.travelArray = [NSMutableArray array];
        self.photoArray = [NSMutableArray array];
    
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"旅行地"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self createScroll];
    [self startRequest];

}

#pragma mark - 网络获取
- (void)startRequest
{
    
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/photos/?count=21&gallery_mode=true", [NSNumber numberWithInteger:self.travId]]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *arr = [dic objectForKey:@"items"];
        for (NSMutableDictionary *sDic in arr) {
            [self.photoArray addObject:[sDic objectForKey:@"photo"]];
            self.model = [[TravelModel alloc] init];
            self.ssDic = [sDic objectForKey:@"poi"];
            [self.model setValuesForKeysWithDictionary:_ssDic];
            [self.travelArray addObject:_model];
            [_model release];
        }
        [self createView];
        [self createImageView];

    }];
    

}


#pragma mark - 创建UIImageView
- (void)createImageView
{
    UIScrollView *sScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, (self.view.frame.size.height - 113) / 3)];
    [sScroll setContentSize:CGSizeMake(sScroll.frame.size.width * [_photoArray count], 0)];
    [sScroll setBackgroundColor:[UIColor redColor]];
    [sScroll setPagingEnabled:YES];
    [sScroll setScrollEnabled:YES];
    [self.travScroll addSubview:sScroll];
    
    for (int i = 0; i < [_photoArray count]; i ++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(sScroll.frame.size.width * i, 0, sScroll.frame.size.width, (self.view.frame.size.height - 113) / 3)];
        [image setImageWithURL:[NSURL URLWithString:[self.photoArray objectAtIndex:i]]];
         
         [sScroll addSubview:image];
         [image release];
    }
    [sScroll release];

}


- (void)createScroll
{
    self.travScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2 * 3)];
    
    [_travScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 2)];
    [_travScroll setShowsHorizontalScrollIndicator:YES];
    [_travScroll setScrollEnabled:YES];
    
    [self.view addSubview:_travScroll];
    [_travScroll release];
}

- (void)createView
{
    CustomTravView *ctView = [[CustomTravView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 113) / 3 + 30, self.view.frame.size.width, self.view.frame.size.height)];
    
    ctView.model = _model;
    ctView.descriptions.text = [_ssDic objectForKey:@"description"];
    
    [_travScroll addSubview:ctView];
    [ctView release];
}











@end
